package kr.jobseeker.movinavi;

import java.net.URLEncoder;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.os.StrictMode;
import android.util.Log;
public class HTMLparserForTheater {
    protected TheaterInfo parser(String theaterName) throws Exception {
    	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

    	String url = new String();
    	String searchingTheater;
    	ArrayList<String> movies = new ArrayList<String>();
    	ArrayList<String> movies_info = new ArrayList<String>();
    	ArrayList<String> times = new ArrayList<String>();
    	TheaterInfo theater = null;
    	searchingTheater = URLEncoder.encode(theaterName, "UTF-8") ;
    	url = "http://www.google.co.kr/movies?hl=ko&q=" + searchingTheater;
    	Log.d("결과", url);
//    	StringBuilder builder = new StringBuilder();
        // get html document structure
        Document document = Jsoup.connect(url).get();
        // selector query
        Elements rows = document.select("div.theater");
//    	Log.d("결과", "test1"+rows.size());
        if(!rows.isEmpty()){
	    	theater = new TheaterInfo(rows.select("div.movie").size());
	    	
//	    	for (int i=0; i<rows.size();i++) {
//	    		theater[i] = new TheaterInfo(rows.select("div.movie").size());
//	    	}
	    	
	    	//데이타 삽입 코드
//	    	int i=0;
	    	int j=0;
	    	Element row = null;
//	    	Log.d("결과", theaterName);
	        for (Element a : rows) {
//	        	Log.d("결과", "test2" + " " + a.getElementsByTag("h2").text());
	        	if(theaterName.compareTo(a.getElementsByTag("h2").text())==0) {
		        	row = a;
		        	break;
	        	}
	        }
	        if(row != null) {
//	        	Log.d("결과", "test3");
		        theater.setTheater(row.getElementsByTag("h2").text());
		        	
		        for (Element row_movies : row.select("div.movie")){
		//        	Log.d("결과", "test3"  + row_movies.select("div.name a").text());
		        	movies.add(row_movies.select("div.name a").text());
		        	movies_info.add(row_movies.select("span.info").text());
		        	for (Element row_times : row_movies.getElementsByAttributeValueMatching("style", "color")){
		//           	Log.d("결과", "test4" + row_times.text());
		        		times.add(row_times.text());
		        	}
		        	theater.setTimes(times, j);
		        	times.clear();
		        	j++;
		        }
		    	theater.setMovies(movies);
		    	theater.setMovies_info(movies_info);
		    	movies.clear();
		    	movies_info.clear();
	//	        i++;
		        j=0;
	//	        }
	        }else {
	        	theater = null;
	        }
	        /*/테스트코드
	        for (int j1=0; j1<theater.length; j1++) {
	      	Log.d("결과", "극장:  " + theater[j1].theater + "\n" );
		      	for (int k=0; k<theater[j1].movies.size(); k++){
			      	Log.d("결과", "영화:  " +theater[j1].movies.get(k).toString()  + "\n"
			      			+ "상영시간:  "+ theater[j1].times[k].toString());
		        }
	        }*/
        }else {
        	Log.d("결과", "암것두없으요");
        }
        return theater;
    }
}
